home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2008 February / PCWFEB08.iso / Software / Freeware / Miro 1.0 / Miro_Installer.exe / xulrunner / python / xpcom / tools / tracer_demo.py < prev   
Encoding:
Python Source  |  2007-11-12  |  4.3 KB  |  114 lines

  1. # ***** BEGIN LICENSE BLOCK *****
  2. # Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3. #
  4. # The contents of this file are subject to the Mozilla Public License Version
  5. # 1.1 (the "License"); you may not use this file except in compliance with
  6. # the License. You may obtain a copy of the License at
  7. # http://www.mozilla.org/MPL/
  8. #
  9. # Software distributed under the License is distributed on an "AS IS" basis,
  10. # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11. # for the specific language governing rights and limitations under the
  12. # License.
  13. #
  14. # The Original Code is Python XPCOM language bindings.
  15. #
  16. # The Initial Developer of the Original Code is
  17. # ActiveState Tool Corp.
  18. # Portions created by the Initial Developer are Copyright (C) 2000, 2001
  19. # the Initial Developer. All Rights Reserved.
  20. #
  21. # Contributor(s):
  22. #  Mark Hammond <mhammond@skippinet.com.au> (original author)
  23. #
  24. # Alternatively, the contents of this file may be used under the terms of
  25. # either the GNU General Public License Version 2 or later (the "GPL"), or
  26. # the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27. # in which case the provisions of the GPL or the LGPL are applicable instead
  28. # of those above. If you wish to allow use of your version of this file only
  29. # under the terms of either the GPL or the LGPL, and not to allow others to
  30. # use your version of this file under the terms of the MPL, indicate your
  31. # decision by deleting the provisions above and replace them with the notice
  32. # and other provisions required by the GPL or the LGPL. If you do not delete
  33. # the provisions above, a recipient may use your version of this file under
  34. # the terms of any one of the MPL, the GPL or the LGPL.
  35. #
  36. # ***** END LICENSE BLOCK *****
  37.  
  38. # This is a demo is how to use the xpcom.server "tracer" facility.
  39. #
  40. # This demo installs a tracer that uses the Python profiler.  It then
  41. # creates the Python test component, and references some methods
  42. # and properties.  It then dumps the profile statistics.
  43.  
  44. # This same technique could also be used for debugging, for example.
  45.  
  46. import profile
  47.  
  48. p = profile.Profile()
  49. getters = {}
  50. setters = {}
  51.  
  52. # A wrapper around a function - looks like a function,
  53. # but actually profiles the delegate.
  54. class TracerDelegate:
  55.     def __init__(self, callme):
  56.         self.callme = callme
  57.     def __call__(self, *args):
  58.         return p.runcall(self.callme, *args)
  59.  
  60. # A wrapper around each of our XPCOM objects.  All PyXPCOM calls
  61. # in are made on this object, which creates a TracerDelagate around
  62. # every function.  As the function is called, it collects profile info.
  63. class Tracer:
  64.     def __init__(self, ob):
  65.         self.__dict__['_ob'] = ob
  66.     def __repr__(self):
  67.         return "<Tracer around %r>" % (self._ob,)
  68.     def __str__(self):
  69.         return "<Tracer around %r>" % (self._ob,)
  70.     def __getattr__(self, attr):
  71.         ret = getattr(self._ob, attr) # Attribute error just goes up
  72.         if callable(ret):
  73.             return TracerDelegate(ret)
  74.         else:
  75.             if not attr.startswith("_com_") and not attr.startswith("_reg_"):
  76.                 getters[attr] = getters.setdefault(attr,0) + 1
  77.             return ret
  78.     def __setattr__(self, attr, val):
  79.         if self.__dict__.has_key(attr):
  80.             self.__dict__[attr] = val
  81.             return
  82.         setters[attr] = setters.setdefault(attr,0) + 1
  83.         setattr(self._ob, attr, val)
  84.  
  85. # Installed as a global XPCOM function that if exists, will be called
  86. # to wrap each XPCOM object created.
  87. def MakeTracer(ob):
  88.     # In some cases we may be asked to wrap ourself, so handle that.
  89.     if isinstance(ob, Tracer):
  90.         return ob
  91.     return Tracer(ob)
  92.  
  93. def test():
  94.     import xpcom.server, xpcom.components
  95.     xpcom.server.tracer = MakeTracer
  96.     contractid = "Python.TestComponent"
  97.     for i in range(100):
  98.         c = xpcom.components.classes[contractid].createInstance().queryInterface(xpcom.components.interfaces.nsIPythonTestInterface)
  99.         c.boolean_value = 0
  100.         a = c.boolean_value
  101.         c.do_boolean(0,1)
  102.     print "Finshed"
  103.     p.print_stats()
  104.     print "%-30s%s" % ("Attribute Gets", "Number")
  105.     print "-" * 36
  106.     for name, num in getters.items():
  107.         print "%-30s%d" % (name, num)
  108.     print "%-30s%s" % ("Attribute Sets", "Number")
  109.     print "-" * 36
  110.     for name, num in setters.items():
  111.         print "%-30s%d" % (name, num)
  112.  
  113. test()
  114.